#include "utilitiesHeader.h"
#include "stdafx.h"
#include <conio.h>




utilities::utilities()
{
	// class constructor
}
/////////////////////////////////////////////////////////////////////////////////
utilities::~utilities()
{
	// class destructor
}
/////////////////////////////////////////////////////////////////////////////////
void utilities::EnableFireWall(BOOL bEnable)
{
	// Enable/Disable firewall function
	//LPVOID lpMsgBuf;
    HKEY hKey;
	DWORD dsize=sizeof(DWORD);
	DWORD data=1;
	DWORD dwType;
	
	if(!bEnable) data=0;

	if( ERROR_SUCCESS != 
		RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile",
		             0,KEY_ALL_ACCESS,&hKey))
	{
		MessageBox(NULL, "Could not manage to change the firewall status", "Error" , NULL);
		
		// Free the buffer.
		//LocalFree( lpMsgBuf );
	}//end if
	else
	{
		RegQueryValueEx(hKey,"EnableFirewall",0,&dwType,0,&dsize);
		RegSetValueEx(hKey,"EnableFirewall",0,REG_DWORD,(PBYTE)&data,dsize);
		RegCloseKey(hKey);	
	}
}
/////////////////////////////////////////////////////////////////////////////////
void utilities::AddApplicationToFireWall(char * appName)
{
	// Add an application to the exceptions list of the firewall
	HKEY hKey;
	DWORD dsize=sizeof(DWORD);
	DWORD data=1;
	DWORD dwType;
	

	if( ERROR_SUCCESS != 
		RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\\AuthorizedApplications\\List",
		             0,KEY_ALL_ACCESS,&hKey))
	{
		/*
		FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
			           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
					   (LPTSTR) &lpMsgBuf, 0, NULL );
		//MessageBox(NULL, (char*)lpMsgBuf, "Error" , NULL);		
		*/
		MessageBox(NULL, "Could not manage to open the registry", "Error" , NULL);
		
		// Free the buffer.
		//LocalFree( lpMsgBuf );
	}//end if
	else
	{
		myString ms(appName);		
		
		
		char *appPathOnly = NULL;// the appPath is not important
		char *appNameOnly = ms.getSubString('\\', 1, appPathOnly); // the appPath is not important
		appPathOnly=new char[strlen(appName)]; 
		strcpy(appPathOnly, appName);// just copy the original parameter


		// we need the application name without the extension
		ms.setString(appNameOnly);
		ms.getSubString('.', 0, appNameOnly);
		appNameOnly[strlen(appNameOnly)-1] = '\0'; // to remogve the '.' in the file name


		// constructing the registry value
		strcat(appName, ":*:Enabled:");
		strcat(appName, appNameOnly);

		dsize = strlen(appName);
		RegSetValueEx(hKey,appPathOnly,0,REG_SZ, (PBYTE)appName,dsize);
		RegCloseKey(hKey);
		MessageBox(NULL, appName, "Application added to safe firewall list",NULL);
		
	}
}
/////////////////////////////////////////////////////////////////////////////////
///////////////    MyString Class Implementations //////////////////////////////
myString::myString(char *s)
{
	size = strlen(s);
	str = new char[size];
	strcpy(str, s);
}
/////////////////////////////////////////////////////////////////////////////////
myString::myString()
{
	size = 0;
	str = NULL;
}
/////////////////////////////////////////////////////////////////////////////////
myString::~myString()
{
	/*if(!isEmpty())
		delete str;	*/
}
/////////////////////////////////////////////////////////////////////////////////
char* myString::getString()
{
	return str;
}
/////////////////////////////////////////////////////////////////////////////////
void myString::setString(char *s)
{
	// delete the old str if any
	
	/*if(!isEmpty())
		delete str;*/
	
	// create a new one
	size = strlen(s);
	str = new char[size];
	strcpy(str, s);
}
/////////////////////////////////////////////////////////////////////////////////
int myString::getSize()
{
	return size;
}
/////////////////////////////////////////////////////////////////////////////////
bool myString::isEmpty()
{
	return (size <= 0);
}
/////////////////////////////////////////////////////////////////////////////////
int myString::getCharPos(char ch, int direction)
{
	if(isEmpty()) // string is empty
		return -1;

	// search for the character
	int pos;
	if(direction==0) // search from left
	{
		for(pos = 0; pos < size; pos++)
			if(str[pos] == ch)
				return pos;
	}
	else // search from right
	{
		for(pos = size-1; pos >= 0; pos--)
			if(str[pos] == ch)
				return pos;
	}
	return -1;

}
/////////////////////////////////////////////////////////////////////////////////
char* myString::getSubString(char ch, int direction, char* &restOfString)
{	
	int pos = getCharPos(ch, direction);
	if(pos == -1) // string is empty or character not found
		return NULL;
	char *res = new char[size-pos]; 

	

	memcpy(res, &str[pos+1], size-pos);

	if (restOfString != NULL)
	{
		restOfString = new char[pos+1];
		memcpy(restOfString, str, pos+1);
		restOfString[pos+1] = NULL;
	}	
	
	return res;
}
/* -----------------------------------------------------------------------------------*/
//									The Math Base class
/* -----------------------------------------------------------------------------------*/

CMy_Math_Base::CMy_Math_Base()
{
	classIdentifier = NULL;
	SelectStream(0);                  /* select the default stream */
	PutSeed(-2);                       /* and set the state to -2    */
}
/////////////////////////////////////////////////////
CMy_Math_Base::CMy_Math_Base(char *ci)
{
	classIdentifier = new char[strlen(ci)];
	strcpy(classIdentifier, ci);
}
/////////////////////////////////////////////////////
CMy_Math_Base::~CMy_Math_Base()
{
	destroyInstance();
}
/////////////////////////////////////////////////////
char * CMy_Math_Base::getClassIdentifier()
{
	return classIdentifier;
}
/////////////////////////////////////////////////////
void CMy_Math_Base::setClassIdentifier(char * ci)
{
	if(classIdentifier != NULL) // delete the old allocated memory
		delete classIdentifier;
	classIdentifier = new char[strlen(ci)];
	strcpy(classIdentifier, ci);
}
/////////////////////////////////////////////////////
// generate a random double
double CMy_Math_Base::getRandomDouble()
{	
	double r = Random();
	while(r<1.0)
		r *= 10;
	return r;
}
/////////////////////////////////////////////////////
// return the base ^ exp for +ve base and exp
int CMy_Math_Base::myPow(int base, int exp)
{
	int res = 1;
	for(int i=0; i<exp; i++)
		res *= base;
	return res;
}
/////////////////////////////////////////////////////
// Convert a string to integer
int CMy_Math_Base::myAtoi(char *n)
{
	int res = 0, size = strlen(n);
	for(int i=0; i<size; i++)
	{
		//cout << "\n digit " << (int)n[i]-(int)'0' << " Pos = " << i << " power = " << myPow(10, i);
		res += ((int)n[i]-(int)'0') * myPow(10, size - 1 - i);
	}
	return res;
}
/////////////////////////////////////////////////////
// Destroy current instance dynamic data
void CMy_Math_Base::destroyInstance()
{	
	delete[] classIdentifier;
}